﻿using System;
using System.Collections.Generic;

namespace AppMain
{
    public class BulletEvent : BaseEvent<BulletEventData>
    {
        //存一下uid 有可能行为中断 需要移除
        private int bulletUID;
        protected override void onProcess()
        {
            ActionRole role = this.Action.role;
            //sql  读取配置
            string selectSql = string.Format("select *from tb_bullet_temp where id='{0}'", this.m_data.tempId);
            var reader = SqlMgr.Instance.select(selectSql);
            float[] boxSize = null;
            float[] offset = null;
            float speed = 0;
            float distance = 0;
            while (reader.Read())
            {
                speed = (float)reader[1];
                distance = (float)reader[2];
                boxSize = Helper.split2Float(reader[3].ToString());
                offset = Helper.split2Float(reader[4].ToString());
            }
            reader.Close();

            long copyUID = role.AttrMap.getAttr<long>(RoleAttr.CopySceneUID).Value;
            long sceneId = role.AttrMap.getAttr<long>(RoleAttr.SceneId).Value;
            sceneId = copyUID > 0 ? copyUID : sceneId;
            Vec3 dir = new Vec3();
            dir.x = role.AttrMap.getAttr<int>(RoleAttr.LookFlag).Value;
            dir.Normalize();
            Vec3 pos = new Vec3(role.box3D.posX + dir.x * offset[0], role.box3D.posY + offset[1], role.box3D.posZ + offset[2]);
            Vec3 size = new Vec3(boxSize[0], boxSize[1], boxSize[2]);

            IAttrMap attrMap = new RoleAttrMap();
            bulletUID = MathUtils.UniqueID;
            attrMap.changeAttr<long>(RoleAttr.RoleId, bulletUID);
            attrMap.changeAttr<int>(RoleAttr.RoleTempId, this.m_data.tempId);
            attrMap.changeAttr<float>(RoleAttr.Speed, speed);
            attrMap.changeAttr<float>(RoleAttr.Distance, distance);
            attrMap.changeAttr<long>(RoleAttr.SceneId, sceneId);
            attrMap.changeAttr<long>(RoleAttr.MasterId, role.UID);
            attrMap.changeAttr<Vec3>(RoleAttr.Position, pos);
            attrMap.changeAttr<Vec3>(RoleAttr.BoxSize, size);
            attrMap.changeAttr<Vec3>(RoleAttr.Dir, dir);
            attrMap.changeAttr<int>(RoleAttr.RoleType, RoleType.Bullet);

            //server创建
            BulletRole bullet = new BulletRole(attrMap);
            //RoleMgr.Instance.addRole(bullet);
            role.AttrMap.getAttr<BaseScene>(RoleAttr.Scene).Value.addRole(bullet);
        }

    }
}
